
import 'package:flutter/material.dart';

/// 获取最小值
double _getMinValue(double value1, double value2) {
  return value1 > value2 ? value2 : value1;
}

/// 通用CheckBox
class ChristmasLightCheckBox extends StatelessWidget {
  final bool value;
  final Widget? trailing;
  final double? iconSize;
  final ValueChanged<bool>? onChanged;

  ChristmasLightCheckBox({
    Key? key,
    required this.value,
    this.trailing,
    this.iconSize,
    this.onChanged,
  }) : super(key: key);

  final String _checkIcon = const AssetImage(
    'assets/images/bonfire_christmas_light_check.png',
  ).keyName;
  final String _unCheckIcon = const AssetImage(
    'assets/images/bonfire_christmas_light_uncheck.png',
  ).keyName;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (_, constraints) {
        bool _isW = constraints.maxWidth.isInfinite;
        bool _isH = constraints.maxHeight.isInfinite;
        double _width = _isW ? 24 : constraints.maxWidth;
        double _height = _isH ? 24 : constraints.maxHeight;
        return UnconstrainedBox(
          child: GestureDetector(
            onTap: () => onChanged?.call(!value),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                Image.asset(
                  value ? _checkIcon : _unCheckIcon,
                  width: iconSize ?? _getMinValue(_width, _height),
                  height: iconSize ?? _getMinValue(_width, _height),
                ),
                const SizedBox(width: 5),
                trailing ?? const SizedBox(),
              ],
            ),
          ),
        );
      },
    );
  }
}

/// 收藏CheckBox
class ChristmasLightFavoriteCheckBox extends StatelessWidget {
  final bool value;
  final double? iconSize;
  final ValueChanged<bool>? onChanged;

  ChristmasLightFavoriteCheckBox({
    super.key,
    required this.value,
    this.iconSize,
    this.onChanged,
  });

  final String _checkIcon = const AssetImage(
    'assets/images/ic_collect_selected.png',
  ).keyName;
  final String _unCheckIcon = const AssetImage(
    'assets/images/ic_collect_unselected.png',
  ).keyName;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (_, constraints) {
        bool _isW = constraints.maxWidth.isInfinite;
        bool _isH = constraints.maxHeight.isInfinite;
        double _width = _isW ? 35 : constraints.maxWidth;
        double _height = _isH ? 35 : constraints.maxHeight;
        return UnconstrainedBox(
          child: GestureDetector(
            onTap: () => onChanged?.call(!value),
            child: Image.asset(
              value ? _checkIcon : _unCheckIcon,
              width: iconSize ?? _getMinValue(_width, _height),
              height: iconSize ?? _getMinValue(_width, _height),
            ),
          ),
        );
      },
    );
  }
}
